package com.example.sunmiv1.dialog.good;

import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentTransaction;

import com.example.sunmiv1.R;
import com.example.sunmiv1.adapter.good.BatchSettingAdapter;
import com.example.sunmiv1.dialog.BaseDialog;
import com.example.sunmiv1.entity.SellcountBean;
import com.example.sunmiv1.utils.net.ACache;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class BatchSettingDialog extends BaseDialog {


    @BindView(R.id.batchSetting_off)
    ImageView batchSettingOff;
    @BindView(R.id.batchSetting_grid)
    GridView batchSettingGrid;

    private BatchSettingAdapter adapter;
    private List<String> list = new ArrayList<>();
    private ArrayList<String> goodsId;
    public static SellcountBean sellcountBean;
    public static int userType;

    public static BatchSettingDialog newInstance(ArrayList<String> goodsId, SellcountBean sellcountBean1,int userType1) {
        sellcountBean=sellcountBean1;
        userType=userType1;
        BatchSettingDialog fragment = new BatchSettingDialog();
        Bundle bundle = new Bundle();
        bundle.putStringArrayList("goods", goodsId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_batch_setting;
    }

    @Override
    protected void initData() {
        isMore=false;
        goodsId = getArguments().getStringArrayList("goods");
        if ("1".equals(sellcountBean.getJurisdiction().getBatch_editgoodsclass())) {
            list.add("商品分類");
        }
        list.add("上下架");
        list.add("店內出售");
        list.add("線上販售");
        list.add("店内會員折扣");
        list.add("線上會員折扣");
        if (userType!=1){
            list.add("線上取貨方式");
            list.add("員工佣金設置");
        }
        if ("1".equals(sellcountBean.getJurisdiction().getBatch_delgoods())) {
            list.add("刪除");
        }
        setAdapter();
    }

    boolean isMore;
    @Override
    public void onResume() {
        super.onResume();
        if (!isMore) {
            isMore = true;
        }else {
            dismiss();
        }
    }


    private void setAdapter() {
        if (adapter == null) {
            adapter = new BatchSettingAdapter(list);
            batchSettingGrid.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void initListener() {
        batchSettingGrid.setOnItemClickListener((parent, view, position, id) -> {
            switch (list.get(position)){
                case "商品分類":
                    call(0);
                    break;
                case "上下架":
                    call(1);
                    break;
                case "店內出售":
                    call(2);
                    break;
                case "線上販售":
                    call(3);
                    break;
                case "店内會員折扣":
                    call(4);
                    break;
                case "線上會員折扣":
                    call(5);
                    break;
                case "線上取貨方式":
                    call(6);
                    break;
                case "員工佣金設置":
                    call(7);
                    break;
                case "刪除":
                    call(8);
                    break;
            }
            dismiss();
        });
    }

    protected void call(int position){
        callback.success((position + 1) + "");
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            // 下面這些設置必須在此方法(onStart())中才有效
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0xff000000));
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            Window window = dialog.getWindow();
            // 如果不設置這句代碼, 那麼彈框就會與四邊都有一定的距離
            window.setBackgroundDrawableResource(android.R.color.transparent);
            window.setWindowAnimations(R.style.DialogFromBottom);// 設置動畫
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = dm.widthPixels;
            params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            params.gravity = Gravity.BOTTOM;
            window.setAttributes(setLayout(params));
        }
    }

    @OnClick(R.id.batchSetting_off)
    public void onViewClicked() {
        dismiss();
    }
}
